﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

using _XNA__RunningDeath.DAO;

using _XNA__RunningDeath.GLO;
namespace _XNA__RunningDeath.GUI
{
    class CGUI_HighScore :CDiaLogBox
    {
        Texture2D _texBackground;
        Texture2D _texBtnBack;
        List<Rectangle> recGameScore = new List<Rectangle>();
        SpriteFont fontFileName;

        Rectangle _recGameDataStand;
        int indexSelected = -1;

        public CGUI_HighScore (ContentManager content, GameTime gametime, SpriteBatch spritebatch):base(content,gametime,spritebatch)
        {
            if(this._content != null)
            {
                this._texBackground = this._content.Load<Texture2D>(@"GUI/bgHighScore");
                this._texBtnBack = this._content.Load<Texture2D>(@"GUI/btnBack"); // vẫn load là zak
                this._texBG = this._content.Load<Texture2D>(@"GUI/guiBG");
                this._dstoado = this._content.Load<Dictionary<string, Rectangle>>(@"GUI/ztoadoXMLgui");

                _recGameDataStand = CThamSo.recDataStand;
                fontFileName = content.Load<SpriteFont>("SpriteFont");

                KhoiTaoRecDiem();
            }
        }

        private void KhoiTaoRecDiem()
        {
            CSaveScore.loadArrData();

            CGLOBAL.recGameScore.Clear();

            _recGameDataStand = new Rectangle(_recGameDataStand.X + 44, _recGameDataStand.Y - 15, _recGameDataStand.Width, _recGameDataStand.Height);
            for (int i = 0; i < CGLOBAL.arrScoreGame.Count; i++)
            {
                if (i == 0)
                {
                    Rectangle recdata = new Rectangle(_recGameDataStand.X, _recGameDataStand.Y, _recGameDataStand.Width, _recGameDataStand.Height);
                    CGLOBAL.recGameScore.Add(recdata);
                }
                else
                {
                    _recGameDataStand.Y = _recGameDataStand.Y + 27;
                    Rectangle recdata = new Rectangle(_recGameDataStand.X, _recGameDataStand.Y, _recGameDataStand.Width, _recGameDataStand.Height);
                    CGLOBAL.recGameScore.Add(recdata);
                }
            }
        }

        public override void Update(GameTime gameTime)
        {
            CGLOBAL.MousePrev = CGLOBAL.MouseCurrent;
            CGLOBAL.MouseCurrent = Mouse.GetState();
            if(CGLOBAL.MouseCurrent.LeftButton==ButtonState.Pressed && CGLOBAL.MousePrev.LeftButton==ButtonState.Released)
            {
                if (this._dstoado["btnBackHightScore"].Contains(new Point(CGLOBAL.MouseCurrent.X, CGLOBAL.MouseCurrent.Y)))
                {
                    CGLOBAL.soundFXbtn.Play(CThamSo.volumSound, CThamSo.pitchSound, CThamSo.panSound);
                    CGLOBAL.NextGUI = true;
                    CGLOBAL.GUISelected = GUIenum.guiMainMenu;
                }

                for (int i = 0; i < recGameScore.Count; i++)
                {
                    if (CGLOBAL.recGameScore[i].Contains(new Point(CGLOBAL.MouseCurrent.X, CGLOBAL.MouseCurrent.Y)))
                    {
                        CGLOBAL.soundFXbtn.Play(CThamSo.volumSound, CThamSo.pitchSound, CThamSo.panSound);
                        indexSelected = i;
                    }
                }
            }
            
        }

        public override void Draw(GameTime gameTime)
        {
            this._spriteBatch.Begin();
            this._spriteBatch.Draw(this._texBG,Vector2.Zero,Color.Gray);
            this._spriteBatch.Draw(this._texBackground, new Vector2(this._dstoado["bgHighScore"].X, this._dstoado["bgHighScore"].Y), Color.White);
            this._spriteBatch.Draw(this._texBtnBack, new Vector2(this._dstoado["btnBackHightScore"].X, this._dstoado["btnBackHightScore"].Y), Color.White);
            
            recGameScore = CGLOBAL.recGameScore;

            for (int i = 0; i < recGameScore.Count; i++)
            {
                string name = addSpace( CGLOBAL.arrScoreGame[i].PlayerName,13);
                string score =centerText( CGLOBAL.arrScoreGame[i].Score.ToString(),7);
                

                if (i == indexSelected)
                {
                    //this._spriteBatch.Draw(this._bgGameSave, recGameScore[i], Color.YellowGreen);
                    _spriteBatch.DrawString(fontFileName, name + " : " + score, new Vector2(recGameScore[i].X, recGameScore[i].Y), Color.White);
                }
                else
                {
                   // this._spriteBatch.Draw(this._bgGameSave, recGameScore[i], Color.White);
                    _spriteBatch.DrawString(fontFileName, name + " : " + score, new Vector2(recGameScore[i].X, recGameScore[i].Y), Color.LightSteelBlue);
                }
               
                
            }
            
            this._spriteBatch.End();
        }



        /// <summary>
        /// Hàm này có chức năng là sẽ add khoảng trắng vào một chuỗi đã truyền vào sao cho chuổi trả về là chuỗi có sl ký tự 
        /// bằng với số lượng ta yêu cầu
        /// </summary>
        /// <param name="s">Chuổi S cần xử lý</param>
        /// <param name="sl">Số lượng ký tự trong chuỗi s cần đạt được</param>
        /// <returns>Chuỗi s sau khi đã xử lý</returns>
        private string addSpace(string s, int sl)
        {
            int count = sl - s.Length;

            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    s += " ";
                }
            }

            return s;
        }

        /// <summary>
        /// hàm này sẽ canh chữ theo số lượng ký tự truyền vào 
        /// ví dụ ta cần số có 3 chữ số mà truyền vào 30 - > _30
        /// </summary>
        /// <param name="s">chuỗi s cần xử lý</param>
        /// <param name="sl">độ rộng của chuỗi cần đạt được nên truyền vào số lẽ</param>
        /// <returns></returns>
        private string centerText(string s, int sl)
        {
            int count = sl - s.Length;
            if (count > 0)
            {
                //int div = count / 2;
                for (int i = 0; i < count; i++)
                {
                    s = "0" + s;
                }
            }
            return s;
        }

    }
}
